import * as THREE from "../../../modules/three/three.module.js";
import { removeArrayItem } from "../util/StaticMethods.js";

const disposeDistance = 100

export default class BulletEntity extends THREE.Mesh {
  initialPos; // 初始位置
  dir;  //方向
  speed = 0.3;
  upDir = new THREE.Vector3(0, 1, 0)
  geometry = new THREE.CapsuleGeometry( 0.03, 1, 4, 8 );
  material = new THREE.MeshBasicMaterial( { color: '#FE5029' } );
  /**
   * @type {AircraftEntity}
   */
  uav

  constructor(initialPos,dir) {
    super();
    this.position.copy(initialPos);
    this.initialPos = initialPos;
    this.dir = dir.clone();
    this.init();
  }

  init() {
    const quaternion = new THREE.Quaternion().setFromUnitVectors(this.upDir, this.dir.normalize());
    this.quaternion.premultiply(quaternion);
  }

  tick() {
    this.position.addScaledVector(this.dir,this.speed)

    if(this.position.distanceTo(this.initialPos) > disposeDistance) {
      this.detachItem();
    }
  }


  detachItem() {
    this.removeFromParent();
    removeArrayItem(this.uav.bullets, this);
  }

}
